package org.example.app.order.validator;

import com.alibaba.cola.exception.Assert;
import org.apache.commons.collections4.CollectionUtils;
import org.example.client.order.dto.cmd.OrderCreateCmd;
import org.example.client.order.dto.cmd.OrderItemCreateCmd;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class OrderValidator {

    public void validateOrderCreateCmd(OrderCreateCmd orderCreateCmd) {
        List<OrderItemCreateCmd> orderItemList = orderCreateCmd.getOrderItemList();
        long itemIdCount = orderItemList.stream().map(OrderItemCreateCmd::getItemId).distinct().count();
        Assert.isTrue(Objects.equals(Math.toIntExact(itemIdCount), CollectionUtils.size(orderItemList)), "商品id不能重复");
    }
}
